// tabBarComponent/tabBar.js
const app = getApp();
const recorderManager = wx.getRecorderManager()

function sendRecord(src, storeId) {
  wx.showToast({
    icon: 'loading',
    title: '正在搜索...',
    duration: 20000
  })
  var obj = {
    //服务器地址
    url: "https://dev.gomoretech.com/demo/newretail-search/api/search/product/queryByVoice",
    filePath: src,
    name: "file",
    header: {
      'Content-Type': 'application/json'
    },
    formData: {
      'storeId': storeId
    },
    success: function (result) {
      console.log(result)
      var data = JSON.parse(result.data);
      if (data.data) {
        try {
          wx.setStorageSync('wj_speechSearch', data.data)
          wx.navigateTo({
            url: '../search/search'
          })
        } catch (e) { }
      } else {
        wx.showToast({
          title: '未检测到语音',
          icon: 'none',
          duration: 2000
        })
      }

    },
    fail: function (err) {
      console.log(err);
      wx.hideToast()
    }
  };
  wx.uploadFile(obj)
}

recorderManager.onStart(() => {
  console.log('recorder start')
})
recorderManager.onPause(() => {
  console.log('recorder pause')
})
recorderManager.onStop((res) => {
  console.log('recorder stop', res)
  const { tempFilePath } = res
})
recorderManager.onFrameRecorded((res) => {
  const { frameBuffer } = res
  console.log('frameBuffer.byteLength', frameBuffer.byteLength)
})

recorderManager.onError((res) => {
  console.log("error", res);
});
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tabbar: {
      type: Object,
      value: {}
    },
    popupShade: {
      type: Boolean,
      value: true
    },
    show: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    // isIphoneX: app.globalData.systemInfo.model == "iPhone X" ? true : false,
    storeId: '',
    recorderType: 'normal', //normal (正常) success (成功) fail (失败) cancel (取消)
    speechStart: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onPopupShade() {
      this.triggerEvent('popup-shade');

      if (this.data.popupShade) {
        this.triggerEvent('popup');
      }
    },
    toSearch: function () {
      console.log("跳转")
    },
    popupVoice: function () {
      console.log("长按")
    },
    // 语音相关
    /**
     * 录音开始
     */
    recorderStart: function (e) {
      this.onPopupShade()
      this.setData({
        speechStart: true,
        storeId: app.globalData.storeInfo.id
      })
      console.log()
      const that = this
      const options = {
        duration: 10000,
        sampleRate: 16000,
        numberOfChannels: 1,
        encodeBitRate: 48000,
        format: 'aac',
        frameSize: 50
      }
      recorderManager.start(options)
      recorderManager.onStart(() => {
        that.setData({
          recorderType: 'success'
        })
      })
    },
    /**
     * 录音取消
     */
    recorderCancel: function (e) {
      this.onPopupShade()
      console.log('录音取消', e)
      recorderManager.stop()
      that.setData({
        recorderType: 'cancel',
        speechStart: false
      })
    },
    /**
     * 触摸移动
     */
    recorderMove: function (e) {

    },
    /**
     * 录音结束
     */
    recorderEnd: function (e) {
      this.onPopupShade()
      console.log("结束")
      const that = this
      that.setData({
        speechStart: false
      })
      let recorderType = that.data.recorderType
      //结束录音  
      recorderManager.stop()
      console.log(recorderType)
      if (recorderType === 'success') {
        recorderManager.onStop((res) => {
          const tempFilePath = res.tempFilePath
          console.log(tempFilePath)
          sendRecord(tempFilePath, this.data.storeId)
        })
      } else {

      }
    },
  }
})
